package com.diver.flinkdemo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diver.flinkdemo.entity.xxt.TerminalUnit;
import com.diver.flinkdemo.enums.DataEnum;
import com.diver.flinkdemo.mapper.mysqlMapper.StudentCardMapper;
import com.diver.flinkdemo.mapper.mysqlMapper.TerminalUnitMapper;
import com.diver.flinkdemo.service.ITerminalUnitService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.jvnet.hk2.annotations.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 终端设备（卡证）
 * @Author: jeecg-boot
 * @Date: 2023-03-07
 * @Version: V1.0
 */
@Slf4j
@Service
public class TerminalUnitServiceImpl extends ServiceImpl<TerminalUnitMapper, TerminalUnit> implements ITerminalUnitService {

    private TerminalUnitMapper mapper;
    private SqlSession session;
    /**
     * 根据卡号、卡类型获取个人终端
     *
     * @param cardNum
     * @param cardType
     * @return
     */
    @Override
    public TerminalUnit getOneByCardNum(String cardNum, int cardType) {
        try {
            log.info("获取初始化好的mysqlSessionFactory-{}", DynamicDataSourceUtil.mysqlSessionFactory);
            log.info("handDalData4");
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession(true);
            mapper = session.getMapper(TerminalUnitMapper.class);
            log.info("handDalData5");
            LambdaQueryWrapper<TerminalUnit> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(TerminalUnit::getCardNum, cardNum);
            queryWrapper.eq(TerminalUnit::getCardType, cardType);
            queryWrapper.eq(TerminalUnit::getDelFlag, DataEnum.NORMAL.getCode());
            queryWrapper.eq(TerminalUnit::getStatus, DataEnum.ENABLE.getCode());
            List<TerminalUnit> terminalUnits = mapper.selectList(queryWrapper);
            log.info("handDalData6");
            if (terminalUnits.size() > 0) {
                return terminalUnits.get(0);
            }
            return null;
        } finally {
//            endOfSession(session);
        }

    }

    /**
     * 更新terminalUnit最新上报时间
     * @param cardNum
     * @param uploadTime
     */
    @Override
    public void updateLastUploadTime(String cardNum, Date uploadTime) {
        //更新terminalUnit最新上报时间
        session = DynamicDataSourceUtil.mysqlSessionFactory.openSession(true);
        TerminalUnitMapper terminalUnitMapper = session.getMapper(TerminalUnitMapper.class);
        LambdaUpdateWrapper<TerminalUnit> params = new LambdaUpdateWrapper<>();
        params.eq(TerminalUnit::getCardNum,cardNum);
        params.set(TerminalUnit::getLastUploadTime,uploadTime);
        terminalUnitMapper.update(null,params);
    }

    @Override
    public void updateLimitMessageByCard(String cardNum, int limitMessageByCard) {
        //更新terminalUnit最新上报时间
        session = DynamicDataSourceUtil.mysqlSessionFactory.openSession(true);
        TerminalUnitMapper terminalUnitMapper = session.getMapper(TerminalUnitMapper.class);
        LambdaUpdateWrapper<TerminalUnit> params = new LambdaUpdateWrapper<>();
        params.eq(TerminalUnit::getCardNum,cardNum);
        params.set(TerminalUnit::getLimitMessageByCard,limitMessageByCard);
        terminalUnitMapper.update(null,params);
    }

    @Override
    public void updateLimitPhoneCard(String cardNum, int limitByPhoneCard) {
        //更新terminalUnit最新上报时间
        session = DynamicDataSourceUtil.mysqlSessionFactory.openSession(true);
        TerminalUnitMapper terminalUnitMapper = session.getMapper(TerminalUnitMapper.class);
        LambdaUpdateWrapper<TerminalUnit> params = new LambdaUpdateWrapper<>();
        params.eq(TerminalUnit::getCardNum,cardNum);
        params.set(TerminalUnit::getLimitByPhoneCard,limitByPhoneCard);
        terminalUnitMapper.update(null,params);
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }
}
